package category

import (
	. "awesomeProject/goods-web/api"
	"awesomeProject/goods-web/forms"
	"awesomeProject/goods-web/global"
	"awesomeProject/goods-web/proto"
	"context"
	"encoding/json"

	"go.uber.org/zap"
	"google.golang.org/protobuf/types/known/emptypb"
	"strconv"

	"github.com/gin-gonic/gin"
	"net/http"
)

func List(ctx *gin.Context) {

	rsp, err := global.GoodsSrvClient.GetAllCategorysList(context.Background(), &emptypb.Empty{})
	if err != nil {
		HandleValidateError(err, ctx)
		return
	}

	data := make([]interface{}, 0)
	err = json.Unmarshal([]byte(rsp.JsonData), &data)

	if err != nil {
		zap.S().Errorf("反序列化失败")
		return
	}

	ctx.JSON(http.StatusOK, data)
}

// 获取商品品类的详细信息 也就是 子品类

func Detail(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		HandleValidateError(err, ctx)
		return
	}

	rspMap := make(map[string]interface{})
	subCategorys := make([]interface{}, 0)

	rsp, err := global.GoodsSrvClient.GetSubCategory(context.Background(), &proto.CategoryListRequest{
		Id: int32(id),
	})
	if err != nil {
		HandleValidateError(err, ctx)
		return
	}
	for _, value := range rsp.SubCategorys {

		subInfo := map[string]interface{}{
			"id":              value.Id,
			"name":            value.Name,
			"level":           value.Level,
			"parent_category": value.ParentCategory,
			"is_tab":          value.IsTab,
		}
		subCategorys = append(subCategorys, subInfo) // 到底要不要地址？
	}

	rspMap["id"] = rsp.Info.Id
	rspMap["name"] = rsp.Info.Name
	rspMap["level"] = rsp.Info.Level
	rspMap["parent_category"] = rsp.Info.ParentCategory
	rspMap["is_tab"] = rsp.Info.IsTab
	rspMap["sub_categories"] = subCategorys

	ctx.JSON(http.StatusOK, rspMap)

}

func New(ctx *gin.Context) {

	categoryForm := forms.CategoryForm{}
	if err := ctx.ShouldBind(&categoryForm); err != nil {
		HandleValidateError(err, ctx)
		return
	}

	rsp, err := global.GoodsSrvClient.CreateCategory(context.Background(), &proto.CategoryInfoRequest{
		Name:           categoryForm.Name,
		Level:          int32(categoryForm.Level),
		ParentCategory: int32(categoryForm.ParentCategory),
		IsTab:          *categoryForm.IsTab,
	})
	if err != nil {
		HandleValidateError(err, ctx)
		return
	}

	// 将响应维护为前端容易读取的数据
	rspMap := make(map[string]interface{})
	rspMap["id"] = rsp.Id
	rspMap["name"] = rsp.Name
	rspMap["level"] = rsp.Level
	rspMap["parent_category"] = rsp.ParentCategory
	rspMap["is_tab"] = rsp.IsTab

	ctx.JSON(http.StatusOK, rspMap)
}

func Delete(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		HandleValidateError(err, ctx)
		return
	}
	// 1.查询到当前id对应的所有子分类
	// 2.然后后删除所有的子分类   进行逻辑删除，因为物理删除，可能会影响其他的数据表

	_, err = global.GoodsSrvClient.DeleteCategory(context.Background(), &proto.DeleteCategoryRequest{
		Id: int32(id),
	})
	if err != nil {
		HandleValidateError(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": "删除完成",
	})
}

func Update(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		HandleValidateError(err, ctx)
		return
	}

	updateForm := forms.UpdateCategoryForm{}
	if err := ctx.ShouldBind(&updateForm); err != nil {
		HandleValidateError(err, ctx)
		return
	}

	request := proto.CategoryInfoRequest{
		Id:   int32(id),
		Name: updateForm.Name,
	}
	// 可能没有传
	if updateForm.IsTab != nil {
		request.IsTab = *updateForm.IsTab
	}

	_, err = global.GoodsSrvClient.UpdateCategory(context.Background(), &request)
	if err != nil {
		HandleValidateError(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": "更新成功",
	})
}
